package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemCompanyIntroBinding;
import com.example.yichuangyun.model.company.CompanyIntroModel;
import com.example.yichuangyun.utils.UiUtils;

import java.util.List;

/**
 * 公司简介
 */
public class CompanyIntroAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<CompanyIntroModel> data;

    public CompanyIntroAdapter(Context context, List<CompanyIntroModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCompanyIntroBinding itemCompanyIntroBinding = ItemCompanyIntroBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(itemCompanyIntroBinding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            CompanyIntroModel model = data.get(position);
            myViewHolder.binding.setCompanyIntroModel(model);

            myViewHolder.binding.tvManagementStatus.setText(UiUtils.getManagementStatus(model.getNewStatusCode()));
            if (!TextUtils.isEmpty(model.getActualCapi())) {
                myViewHolder.binding.tvReallyCapital.setText(context.getString(R.string.really_capital, model.getActualCapi()));
            }
            if (null != model.getIndustryCodeList() && model.getIndustryCodeList().size() > 0) {
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : model.getIndustryCodeList()) {
                    stringBuilder.append(s).append(",");
                }
                myViewHolder.binding.tvIndustry.setText(stringBuilder.substring(0, stringBuilder.toString().length() - 1));
            }
            if (!TextUtils.isEmpty(model.getReportAddress())) {
                if (!TextUtils.isEmpty(model.getReportYear())) {
                    myViewHolder.binding.tvReportAddress.setText(Html.fromHtml(context.getString(R.string.report_address, model.getReportAddress(), model.getReportYear()), Html.FROM_HTML_MODE_LEGACY));
                } else {
                    myViewHolder.binding.tvReportAddress.setText(model.getReportAddress());
                }
            }
            if (!TextUtils.isEmpty(model.getTermStart())) {
                if (!TextUtils.isEmpty(model.getTermEnd())) {
                    myViewHolder.binding.tvTerm.setText(context.getString(R.string.term_range, model.getTermStart(), model.getTermEnd()));
                } else {
                    myViewHolder.binding.tvTerm.setText(context.getString(R.string.no_term, model.getTermStart()));
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemCompanyIntroBinding binding;

        public MyViewHolder(ItemCompanyIntroBinding itemCompanyIntroBinding) {
            super(itemCompanyIntroBinding.getRoot());
            binding = itemCompanyIntroBinding;
        }
    }
}
